namespace two {
    export class HttpOperator {
        public constructor() {
        }
        private url:string = null;
        private static instance: HttpOperator = null;

        public static Instance() {
            if (!this.instance)
                this.instance = new HttpOperator();
            return this.instance;
        }

        public setGameUrl(url:string){
            this.url = url;
        }
        public sendRequest(url: string, data: Object, call: (data: any) => void) {
            var dataStr = "";
            for (let key in data) {
                dataStr += key + "=" + data[key] + "&";
            }
            if (dataStr != "") {
                dataStr = dataStr.substr(0, dataStr.length - 1);
            }

            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            //request.responseType = egret.HttpResponseType.ARRAY_BUFFER;
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.open(url, egret.HttpMethod.POST);
            request.send(dataStr);
            
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                if (!request.response) {
                    console.log("error " + url);
                    return;
                }

                let respone = request.response;
                
                var reqObj = JSON.parse(respone);
                console.log(reqObj);
                if (call) {
                    call(reqObj);
                }
                
                call = null;
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                console.log("get error : " + event);
                call = null;
            }, this);
            request.addEventListener(egret.ProgressEvent.PROGRESS, (event: egret.ProgressEvent) => {
                console.log("get progress : " + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
            }, this);
        }

        public onSendCommonRequest(apiName: string, data: any, call: (data: any) => void, url:string = "") {
            //console.log(apiName, data);
            /*
            if (one.getOption('debug') == "1") {
                var response = RES.getRes(apiName + "_json");
                egret.setTimeout(() => {
                    console.log(response);
                    call(response);
                    call = null;
                }, this, 20);
                return;
            }
            */

            let apis = apiName.split("_");

            var postData = {};
            postData['data'] = JSON.stringify(data);
            if (apiName != "User_login") {
                postData["token"] = one.getOption("token") || "sI23ZIXeuty1poutg6d9ow";
            }

            this.sendRequest("http://pk23.g-demos.com/xingkong/" + apis[0] + "." + apis[1], postData, call);
        }

        static URL:string = "";
    }

    
    // export let operator = new HttpOperator();
}